<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件上传</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link rel="shortcut icon" href="favicon.ico" th:href="@{favicon.ico}">
    <link href="/css/bootstrap.min.css?v=3.3.6" th:href="@{/css/bootstrap.min.css?v=3.3.6}" rel="stylesheet">
    <link href="/css/font-awesome.css?v=4.4.0" th:href="@{/css/font-awesome.css?v=4.4.0}" rel="stylesheet">
    <link href="/css/animate.css" th:href="@{/css/animate.css}" rel="stylesheet">
    <link href="/css/layui.css" th:href="@{/css/layui.css}" rel="stylesheet">
    <link href="/css/style.css?v=4.1.0" th:href="@{/css/style.css?v=4.1.0}" rel="stylesheet">
    <script th:inline="javascript">
        var ctx = [[@{/}]] ;
    </script>
</head>
<body class="gray-bg">
<div class="wrapper wrapper-content" id="app">
    <div class="row">
        <div class="col-sm-3">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <div class="file-manager">
                        <input id="id" name="id" th:value="${tMagazine.id}" type="hidden">
                        <input id="titlePage" name="titlePage" th:value="${tMagazine.titlePage}" type="hidden">
                        <button type="button" class="layui-btn" id="test1">
                            <i class="fa fa-cloud"></i>上传图片
                        </button>
                        <button type="button" class="layui-btn" style="background-color: #ed5565" onclick="removeContent()">
                            <i class="fa fa-trash-o"></i>清空图片
                        </button>
                        <div class="hr-line-dashed"></div>

                        <div id="titlePageBox" class="file-box">
                            <div class="file">
                                <a href="#">
                                    <span class="corner"></span>
                                    <div class="image">
                                        <img id="fmImg" alt="image" class="img-responsive" th:src="${tMagazine.titlePage}">
                                    </div>
                                    <div class="file-name">
                                        <small>封面</small>
                                    </div>
                                    &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<button
                                        class="btn btn-warning btn-xs copy"  id="fmBtn" th:url="${tMagazine.titlePage}">复制
                                </button>&nbsp; &nbsp; &nbsp; &nbsp;<button class="btn btn-danger btn-xs"
                                                                            onclick="removeTitle()">删除
                                </button>
                                </a>
                            </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>
        <div class="col-sm-9 animated fadeInRight">
            <div class="row">
                <div class="col-sm-12">
                    <div class="file-box" v-for="row in rows">
                        <div class="file">
                            <a href="#">
                                <span class="corner"></span>
                                <div class="image">
                                    <img alt="image" class="img-responsive" :src="row.picture">
                                </div>
                                <div class="file-name">
                                    <small>第{{row.pageNo}}页</small>
                                </div>
                                &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<button
                                    class="btn btn-warning btn-xs copy" :url="row.picture">复制
                            </button>&nbsp; &nbsp; &nbsp; &nbsp;<button class="btn btn-danger btn-xs"
                                                                        @click="remove(row.id)">删除
                            </button>
                            </a>
                        </div>
                    </div>
                    <div id="incomeNum"></div>
                </div>
            </div>
            <div>
                <ul id="page"></ul>
            </div>
        </div>
    </div>
</div>

<!-- 全局js -->
<script src="/js/jquery.min.js?v=2.1.4" th:src="@{/js/jquery.min.js?v=2.1.4}"></script>
<script src="/js/bootstrap.min.js?v=3.3.6" th:src="@{/js/bootstrap.min.js?v=3.3.6}"></script>

<!-- 自定义js -->
<script src="/js/content.js?v=1.0.0" th:src="@{/js/content.js?v=1.0.0}"></script>

<script src="/js/bootstrap-paginator.min.js" th:src="@{/js/bootstrap-paginator.min.js}"></script>

<script src="/js/layui.js" th:src="@{/js/layui.js}"></script>
<script src="/js/plugins/clipboard/clipboard.min.js" th:src="@{/js/plugins/clipboard/clipboard.min.js}"></script>
<script src="/js/plugins/layer/layer.min.js" th:src="@{/js/plugins/layer/layer.min.js}"></script>
<script src="/js/vue.min.js" th:src="@{/js/vue.min.js}"></script>

<script>
    var app = new Vue({
        el: '#app',
        data: {
            limit: 12,
            offset: 0,
            total: 0,
            file: '',
            type: '',
            rows: '',
        },
        methods: {
            getData: function () {
                $.getJSON(ctx+"system/tMagazine/pictures", {
                    limit: this.limit,
                    offset: this.offset,
                    sort: 'page_no',
                    order: 'asc',
                    magazineId: $('#id').val(),
                    type: this.type
                }, function (r) {
                    app.total = r.total;
                    app.rows = r.rows;
                    app.page();
                });
            },
            page: function () {
                var options = {
                    currentPage: app.offset / 12 + 1, //当前页
                    totalPages: Math.ceil(app.total / 12), //总页数
                    numberofPages: 4, //显示的页数
                    bootstrapMajorVersion: 3,
                    alignment: 'center',
                    size: 'large',
                    shouldShowPage: true,
                    itemTexts: function (type, page, current) { //修改显示文字
                        switch (type) {
                            case "first":
                                return "首页";
                            case "prev":
                                return "上一页";
                            case "next":
                                return "下一页";
                            case "last":
                                return "尾页";
                            case "page":
                                return page;
                        }
                    },
                    onPageClicked: function (event, originalEvent, type, page) {
                        app.offset = (page - 1) * 12;
                        app.getData();
                    }
                };
                $('#page').bootstrapPaginator(options);
            },
            remove: function (id) {
                layer.confirm('确定要删除选中的记录？', {
                    btn: ['确定', '取消']
                }, function () {
                    $.ajax({
                        url: ctx+"system/tMagazine/pictures/remove",
                        type: "post",
                        data: {
                            'id': id,
                            'type': 1
                        },
                        success: function (r) {
                            if (r.code == 0) {
                                layer.msg(r.msg);
                                if (null!=r.title) {
                                    $('#fmImg').attr('src',r.title);
                                    $('#fmBtn').attr('url',r.title);
                                    $('#titlePageBox').show();
                                }else {
                                    $('#titlePageBox').hide();
                                }
                                app.getData();
                            } else {
                                layer.msg(r.msg);
                                app.getData();
                            }
                        }
                    });
                })
            },
            changeType: function (i) {
                this.type = i;
                this.offset = 0;
                this.getData();
            }
        },
        created: function () {
            this.changeType('')
        }
    });
</script>
<script type="text/javascript">
    $(document).ready(function() {
        var titlePage = $('#titlePage').val();
        if (null==titlePage || ''===titlePage){
            $('#titlePageBox').hide();
        } else {
            $('#titlePageBox').show();
        }

    });

    var clipboard = new Clipboard('button.copy', {
        text: function (trigger) {
            layer.msg('文件路径已复制到粘贴板');
            return $(trigger).attr('url');
        }
    });
    layui.use('upload', function () {
        var upload = layui.upload;
        //执行实例
        var uploadInst = upload.render({
            elem: '#test1', //绑定元素
            url: ctx+'system/tMagazine/pictures/upload', //上传接口
            data: {
                id: $('#id').val()
            },
            size: 30720,
            accept: 'images',
            exts: 'jpg|png|jpeg',
            multiple: true,
            done: function (r) {
                layer.msg(r.msg);
                if (null!=r.title) {
                    $('#fmImg').attr('src',r.title);
                    $('#fmBtn').attr('url',r.title);
                    $('#titlePageBox').show();
                }else {
                    $('#titlePageBox').hide();
                }
                app.getData();
            },
            error: function (r) {
                layer.msg(r.msg);
            }
        });
    });

    function removeTitle() {
        layer.confirm('确定要删除选中的记录？', {
            btn : [ '确定', '取消' ]
        }, function() {
            var id = $('#id').val();
            if (null==id){
                layer.msg('杂志对象不存在');
                return;
            }
            $.ajax({
                url : ctx+"system/tMagazine/pictures/remove",
                type : "post",
                data : {
                    'id': id,
                    'type': 0
                },
                success : function(r) {
                    if (r.code==0) {
                        layer.msg(r.msg);
                        if (null!=r.title) {
                            $('#fmImg').attr('src',r.title);
                            $('#fmBtn').attr('url',r.title);
                            $('#titlePageBox').show();
                        }else {
                            $('#titlePageBox').hide();
                        }
                        app.getData();
                    }else{
                        layer.msg(r.msg);
                    }
                }
            });
        })
    }

    function removeContent() {
        layer.confirm('确定要清空图片？', {
            btn : [ '确定', '取消' ]
        }, function() {
            var id = $('#id').val();
            if (null==id){
                layer.msg('杂志对象不存在');
                return;
            }
            $.ajax({
                url : ctx+"system/tMagazine/pictures/remove",
                type : "post",
                data : {
                    'id': id,
                    'type': 2
                },
                success : function(r) {
                    if (r.code==0) {
                        layer.msg(r.msg);
                        $('#titlePageBox').hide();
                        app.getData();
                        $('#page').bootstrapPaginator(null);
                    }else{
                        layer.msg(r.msg);
                    }
                }
            });
        })
    }

</script>
</body>
</html>
